package com.qincji.webviewbrowser;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;

import java.util.ArrayList;

/**
 * 描述：
 * 作者：qincji on 2021/6/25 10:56
 * 起于：https://zqxq.club
 * 邮箱：xhunmon@126.com
 */
public class MoreDialog extends CommonDialog {

    private static final String TAG = "MoreDialog";
    private Context context;
    private OnClickItemListener listener;

    private MoreDialog(Context context, OnClickItemListener listener) {
        super(context);
        this.context = context;
        this.listener = listener;
        build();
    }

    private void build() {
        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_common_more_dialog, null, false);
        //这玩意要在setContentCustomComponent前设置有效？
        setSize(DependentLayout.LayoutConfig.MATCH_CONTENT, DependentLayout.LayoutConfig.MATCH_CONTENT);
        setAlignment(LayoutAlignment.RIGHT | LayoutAlignment.TOP);
        setContentCustomComponent(component);
        XRecyclerView recyclerView = (XRecyclerView) component.findComponentById(ResourceTable.Id_recyclerView);
        ArrayList<MoreModel> moreModels = new ArrayList<>();
        moreModels.add(new MoreModel(ResourceTable.Graphic_share, "分享"));
        moreModels.add(new MoreModel(ResourceTable.Graphic_open_browser, "在浏览器打开"));
        moreModels.add(new MoreModel(ResourceTable.Graphic_refresh, "刷新"));
        moreModels.add(new MoreModel(ResourceTable.Graphic_back, "后退"));
        moreModels.add(new MoreModel(ResourceTable.Graphic_forward, "前进"));
        moreModels.add(new MoreModel(ResourceTable.Graphic_home, "退出"));
        moreModels.add(new MoreModel(ResourceTable.Graphic_close, "关闭"));
        MoreAdapter<MoreModel> adapter = new MoreAdapter<>(context, moreModels);
        adapter.setOnItemClickListener(new XRecyclerView.RecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Component component, int i) {
                dealClick(i);
            }
        });
        recyclerView.setItemProvider(adapter);
        recyclerView.refresh();
        setTransparent(true);
        setAutoClosable(true);// 点击空白处自动关闭确认框
    }

    private void dealClick(int index) {
        listener.click(index);
        destroy();
    }

    public interface OnClickItemListener {
        void click(int item);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        context = null;
        listener = null;
    }

    public static void showPop(Context context, OnClickItemListener listener) {
        new MoreDialog(context, listener).show();
    }

}